﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OsPortal;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Text;

public partial class UserControl_dang_tin : System.Web.UI.UserControl
{
    protected string strClassName = "UserControl_dang_tin";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!clsUsers.CheckLoginUser_Client())
        {
            img_youtube.Visible = false;
            thong_bao_dang_tin.Visible = true;
        }
        else thong_bao_dang_tin.Visible = false;

        xSetMod();
        if (!IsPostBack)
        {
            xgetKieuPhong();
            xgetTinhThanh();
        }
    }
    /// <summary>
    /// Set MOD
    /// </summary>
    private void xSetMod()
    {
        xsetTime();
        string strPT_ID = clsCommon.fncGetRoute(this.Page, "param1");
        if (!String.IsNullOrEmpty(strPT_ID))
        {
            hdMod.Value = "edit";
            ltrTitle.Text = "Sửa nội dung tin";
            btnDangTin.Text = "Cập nhật";
            xSetDataPhongTro();
        }
        else
        {
            hdMod.Value = "add";
            ltrTitle.Text = "Đăng tin";
            btnDangTin.Text = "Đăng tin";
        }
    }

    /// <summary>
    /// Lấy thông tin chi tiết của phòng trọ nếu đang trong trạng thái update
    /// </summary>
    private void xSetDataPhongTro()
    {
        string strPT_ID = clsCommon.fncGetRoute(this.Page, "param1");
        DataTable dt = clsPhongTro.PhongTro_FND(1, 1, "", 0, 0, strPT_ID, "", "", 2).Tables[0];
        if (dt == null) return;
        if (dt.Rows.Count <= 0) return;
        //Kiểm tra tin này có phải của thằng này đăng không?
        string strUS_ID = dt.Rows[0]["US_ID"].ToString();
        if (strUS_ID != clsUsers.Users_GetValueCooKie(basConst.COOKIE_USER_ID))
        {
            Response.Redirect("/users-manager/quan-ly-su-kien");
        }
        txtTieuDe.Text = dt.Rows[0]["pt_tieude"].ToString();
        ddlChuyenMuc.SelectedValue = dt.Rows[0]["KP_ID"].ToString();
        txtNoiDung.Text = System.Net.WebUtility.HtmlDecode(OsPortal.oStringHelper.ClearTagHTML(dt.Rows[0]["pt_motachitiet"].ToString()));
        txtYoutube.Text = dt.Rows[0]["pt_video_youtube"].ToString();
        ddlTinhThanh.SelectedValue = dt.Rows[0]["KV_PARENTID"].ToString();
        hdQuanHuyen.Value = dt.Rows[0]["KV_ID"].ToString().ToLower();
        txtDiaChi.Text = dt.Rows[0]["pt_diachi"].ToString();
        txtDienThoai.Text = dt.Rows[0]["pt_dienthoai"].ToString();
        txtGiaPhong.Text = dt.Rows[0]["pt_giaphong"].ToString();
        //  txtDienTich.Text = dt.Rows[0]["pt_dientich"].ToString();
        //
        hdKinhDo.Value = dt.Rows[0]["pt_kinhdo"].ToString();
        hdViDo.Value = dt.Rows[0]["pt_vido"].ToString();
        //
        hdAnh1.Value = dt.Rows[0]["pt_anh1"].ToString();
        txtNgayKetThuc.Text = dt.Rows[0]["pt_ngayketthuc"].ToString();
        txtNgayBatDau.Text = dt.Rows[0]["pt_ngaybatdau"].ToString();
        ddl_GioBatDau.SelectedValue = dt.Rows[0]["pt_giobatdau"].ToString();
        ddl_PhutBatDau.SelectedValue = dt.Rows[0]["pt_phutbatdau"].ToString();
        ddl_GioKetThuc.SelectedValue = dt.Rows[0]["pt_gioketthuc"].ToString();
        ddl_PhutKetThuc.SelectedValue = dt.Rows[0]["pt_phutketthuc"].ToString();
    }

    /// <summary>
    /// Lấy danh sách tỉnh thành trên toàn quốc
    /// Author: Nguyen7huan
    /// Create: 26/09/2013
    /// </summary>
    private void xgetTinhThanh()
    {
        try
        {
            ddlTinhThanh.DataSource = clsKhuVuc.fncGetKhuVucBy_ParentID("0");
            ddlTinhThanh.DataTextField = "kv_ten";
            ddlTinhThanh.DataValueField = "KV_ID";
            ddlTinhThanh.DataBind();

        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "getKieuPhong", ex.ToString());
        }
    }

    private void xsetTime()
    {
        ddl_GioBatDau.AppendDataBoundItems = true;
        ddl_PhutBatDau.AppendDataBoundItems = true;
        //Gio bat dau va gio ket thuc
        for (int i = 0; i <= 24; i++)
        {
            if (i < 10)
            {
                ddl_GioBatDau.Items.Add(new ListItem("0" + i + "", "" + i + ""));
                ddl_GioKetThuc.Items.Add(new ListItem("0" + i + "", "" + i + ""));
            }
            else
            {
                ddl_GioBatDau.Items.Add(new ListItem("" + i + "", "" + i + ""));
                ddl_GioKetThuc.Items.Add(new ListItem("" + i + "", "" + i + ""));
            }
        }

        ddl_PhutBatDau.Items.Add(new ListItem("00", "00"));
        ddl_PhutBatDau.Items.Add(new ListItem("15", "15"));
        ddl_PhutBatDau.Items.Add(new ListItem("30", "30"));
        ddl_PhutBatDau.Items.Add(new ListItem("45", "45"));

        ddl_PhutKetThuc.Items.Add(new ListItem("00", "00"));
        ddl_PhutKetThuc.Items.Add(new ListItem("15", "15"));
        ddl_PhutKetThuc.Items.Add(new ListItem("30", "30"));
        ddl_PhutKetThuc.Items.Add(new ListItem("45", "45"));



    }
    /// <summary>
    /// Lây dữ liệu post lên
    /// </summary>
    /// <returns></returns>
    private clsPhongTro.stPhongTro getValuePost()
    {
        clsPhongTro.stPhongTro objPhongTro = new clsPhongTro.stPhongTro();
        string strus_id = clsUsers.Users_GetValueCooKie(basConst.COOKIE_USER_ID);
        bool blpt_trangthai = true;
        //
        if (!clsUsers.CheckLoginUser_Client())
        {
            strus_id = "US00000";
            blpt_trangthai = false;
        }
        //
        try
        {
            int widthImages = Convert.ToInt16(OsPortal.oStringHelper.fncGetAppSettings("DO_RONG_ANH_UPlOAD"));
            //
            objPhongTro.PT_ID = clsCommon.fncGetRoute(this.Page, "param1");
            objPhongTro.US_ID = strus_id;
            objPhongTro.pt_tieude = OsPortal.oStringHelper.fncCnvNullToString(txtTieuDe.Text.Trim());
            objPhongTro.KP_ID = OsPortal.oStringHelper.fncCnvNullToString(ddlChuyenMuc.SelectedValue);
            string strContent = OsPortal.oStringHelper.fncCnvNullToString(txtNoiDung.Text.Trim());
            //objPhongTro.pt_motachitiet = OsPortal.oStringHelper.fncCnvNullToString(txtNoiDung.Text.Trim());
            //--------------Replace Keyword----------------
            //Stopwatch s = new Stopwatch();
            //s.Start();
            //StringBuilder sb = new StringBuilder(strContent);
            //sb = sb.Replace("Hà Nội", "<a href='/cho-thue-phong-tro/ha-noi' title='cho thue phong tro ha noi'>Hà Nội</a>");
            //sb = sb.Replace("Cầu Giấy", "<a href='/cho-thue-phong-tro/cau-giay' title='cho thue phong tro cau giay'>Cầu Giấy</a>");
            //sb = sb.Replace("Hoàng Mai", "<a href='/cho-thue-phong-tro/hoang-mai' title='cho thue phong tro hoang mai'>Hoàng Mai</a>");
            //sb = sb.Replace("Thanh Xuân", "<a href='/cho-thue-phong-tro/thanh-xuan' title='cho thue phong tro thanh xuan'>Thanh Xuân</a>");
            // string strContentOutput = sb.ToString();
            string strContentOutput = strContent;
            //s.Stop();
            //---------------------------------------------
            objPhongTro.pt_motachitiet = strContentOutput;
            objPhongTro.KV_ID = OsPortal.oStringHelper.fncCnvNullToString(Request.Form["ddlQuanHuyen"]);
            objPhongTro.pt_diachi = OsPortal.oStringHelper.fncCnvNullToString(txtDiaChi.Text.Trim());
            objPhongTro.pt_dienthoai = OsPortal.oStringHelper.fncCnvNullToString(txtDienThoai.Text.Trim());
            objPhongTro.pt_giaphong = OsPortal.oStringHelper.fncCnvNullToInt(txtGiaPhong.Text.Trim());
            // objPhongTro.pt_dientich = OsPortal.oStringHelper.fncCnvNullToInt(txtDienTich.Text.Trim());
            //
            objPhongTro.pt_kinhdo = OsPortal.oStringHelper.fncCnvNullToString(Request.Form["kinhdo"]);
            objPhongTro.pt_vido = OsPortal.oStringHelper.fncCnvNullToString(Request.Form["vido"]);
            //--------------------------Full Control--------------------------------------------------------
            if (clsUsers.CheckLoginUser_Client())
            {
                //Ảnh 1
                string strAnh1 = hdAnh1.Value;
                if (fulAnh1.HasFile)
                {
                    string strOutImagesName;
                    OsPortal.oFileHelper.UploadAnhResizeImage(fulAnh1, basConst.PATH_IMAGES_USER, widthImages, out strOutImagesName, OsPortal.oStringHelper.fncGetAppSettings("KEYWORD_CHEN_VAO_ANH"), true);
                    if (!string.IsNullOrEmpty(strOutImagesName))
                        strAnh1 = basConst.PATH_IMAGES_USER.Replace("~/", "") + strOutImagesName;

                }
                objPhongTro.PT_Anh1 = strAnh1;
                objPhongTro.PT_Video_Youtube = OsPortal.oStringHelper.fncCnvNullToString(txtYoutube.Text.Trim());
                objPhongTro.pt_ngaybatdau = txtNgayBatDau.Text.Trim();
                objPhongTro.pt_ngayketthuc = txtNgayKetThuc.Text.Trim();
                objPhongTro.pt_phutbatdau = ddl_PhutBatDau.SelectedValue;
                objPhongTro.pt_phutketthuc = ddl_PhutKetThuc.SelectedValue;
                objPhongTro.pt_giobatdau = ddl_GioBatDau.SelectedValue;
                objPhongTro.pt_gioketthuc = ddl_GioKetThuc.SelectedValue;
            }
            //--------------------------------------------------------------------------------------------------
            objPhongTro.pt_trangthai = blpt_trangthai;
        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "getValuePost", ex.ToString());
        }
        return objPhongTro;
    }

    /// <summary>
    /// Lấy danh sách kiểu phòng trọ
    /// </summary>
    private void xgetKieuPhong()
    {
        try
        {
            ddlChuyenMuc.DataSource = clsKieuPhongTro.fncKieuPhongTro_FND("", "");
            ddlChuyenMuc.DataTextField = "kp_ten";
            ddlChuyenMuc.DataValueField = "KP_ID";
            ddlChuyenMuc.DataBind();

        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "getKieuPhong", ex.ToString());
        }
    }
    protected void btnDangTin_Click(object sender, EventArgs e)
    {
        string strCapCha = OsPortal.oStringHelper.fncCnvNullToString(Request.Form["txtCapchaHash"]);
        string strCapChaInput = OsPortal.oStringHelper.rpHash(OsPortal.oStringHelper.fncCnvNullToString(Request.Form["txtCapcha"]));
        hdQuanHuyen.Value = OsPortal.oStringHelper.fncCnvNullToString(Request.Form["ddlQuanHuyen"]);
        if (strCapCha == strCapChaInput)
        {
            string strPT_ID = clsCommon.fncGetRoute(this.Page, "param1");
            //SetMod
            string strMod = basConst.MOD_ADD;
            string strMessage = "Đăng tin thành công";
            if (!String.IsNullOrEmpty(strPT_ID))
            {
                strMod = basConst.MOD_UPDATE;
                strMessage = "Cập nhật thành công";
            }
            string strPT_ID_Se_Dang = clsCommon.fncAutoCreateID("PT", "phongtro", "PT_ID");
            string strResult = clsPhongTro.fncPhongTro_Act(getValuePost(), strMod);
            if (strResult != "")
            {
                OsPortal.oHtmlHelper.Alert(strResult, this.Page);
            }
            else
            {
                if (!clsUsers.CheckLoginUser_Client())
                {
                    OsPortal.oHtmlHelper.AlertAndRederect(strMessage, "/kich-hoat-tin-sms/" + strPT_ID_Se_Dang, this.Page);
                }
                else
                {
                    OsPortal.oHtmlHelper.AlertAndRederect(strMessage, "/users-manager/quan-ly-su-kien", this.Page);
                }
            }
        }
        else
        {
            OsPortal.oHtmlHelper.Alert("Mã an toàn không chính xác", this.Page);
        }

    }
}